//
//  WalletRecordViewController.m
//  Molian
//
//  Created by xiaocankeji on 15/7/23.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "WalletRecordViewController.h"
#import "WalletRecordCell.h"
#import "MLWithdraw.h"
#import "MLConsumeRecord.h"
// 每次拉取的用户数目
#define LoadDataCount 50
@interface WalletRecordViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    Utils *promptView;
}
//@property (nonatomic , strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *records;//记录
@end

@implementation WalletRecordViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // 从导航条以下开始计算
  //  self.automaticallyAdjustsScrollViewInsets = NO;
//    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, DeviceWidth, DeviceHeight-64) style:UITableViewStylePlain];
//    self.tableView.backgroundColor = [UIColor whiteColor];
//    self.tableView.delegate = self;
//    self.tableView.dataSource = self;
//    self.tableView.separatorColor = RGBA(133, 133, 133, 1);//分隔条颜色
//    self.tableView.tableFooterView = [[UIView alloc] init];
//    [self.view addSubview:self.tableView];
    
    [self queryData];
    
    
    // Do any additional setup after loading the view.
}
//上拉
- (void)upPullLoadData
{
    if(self.records.count<LoadDataCount/2){
        [self.tableView footerEndRefreshing];
        return;
    }
    [self queryData];
    
}
//下拉
- (void)slimeRefreshStartRefresh:(SRRefreshView *)refreshView
{
    //加载数据
    self.currentPage = 0;
    [self queryData];
}
- (NSMutableArray *)records
{
    if (_records == nil) {
        _records = [NSMutableArray array];
    }
    return _records;
}
- (void)queryData
{
    NSString *str;
    if ([self.title isEqualToString:@"提现记录"]){
        str = @"提现记录";
        AVQuery *query = [MLWithdraw query];
        [query whereKey:@"user" equalTo:[MLUser currentUser]];
        [query orderByDescending:@"createdAt"];
        query.limit = LoadDataCount;
        query.skip = self.currentPage * LoadDataCount;
        [HUD setOnViewWithTitle:@"加载中" animated:YES];
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            [HUD hideFromViewAnimated:YES];
            [self.tableView footerEndRefreshing];
            [self.slimeView endRefresh];
            if (error) {
                if (self.records.count== 0) {
                    promptView.viewPrompt.hidden = NO;
                }
            }else{
                if (self.currentPage == 0) {
                    [self.records removeAllObjects];
                }
                for (MLWithdraw *withdraw in objects) {
                    [self.records addObject:withdraw];
                }
                if (self.records.count== 0) {
                    promptView.viewPrompt.hidden = NO;
                }else{
                    self.currentPage++;
                    promptView.viewPrompt.hidden = YES;
                    [self.tableView reloadData];
                }
            }
            
        }];
        //
    }else if([self.title isEqualToString:@"充值记录"]){
        str = @"充值记录";
        //充值记录MLConsumeRecord
        AVQuery *query = [MLConsumeRecord query];
        [query orderByDescending:@"createdAt"];
        [query whereKey:@"user" equalTo:[MLUser currentUser]];
        query.limit = LoadDataCount;
        query.skip = self.currentPage * LoadDataCount;
        [HUD setOnViewWithTitle:@"加载中" animated:YES];
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            [HUD hideFromViewAnimated:YES];
            [self.tableView footerEndRefreshing];
            [self.slimeView endRefresh];
            if (error) {
                if (self.records.count == 0) {
                    promptView.viewPrompt.hidden = NO;
                }
            }else{
                if (self.currentPage == 0) {
                    [self.records removeAllObjects];
                }
                for (MLConsumeRecord *withdraw in objects) {
                    [self.records addObject:withdraw];
                }
                if (self.records.count == 0) {
                    promptView.viewPrompt.hidden = NO;
                }else{
                    self.currentPage++;
                    promptView.viewPrompt.hidden = YES;
                    [self.tableView reloadData];
                }
            }
            
        }];
    }else if([self.title isEqualToString:@"收支记录"]){
        str = @"收支记录";
        //充值记录MLConsumeRecord
        AVQuery *query = [MLIncomeRecord query];
    
        [query orderByDescending:@"createdAt"];
        [query whereKey:@"user" equalTo:[MLUser currentUser]];
        query.limit = LoadDataCount;
        query.skip = self.currentPage * LoadDataCount;
        [query includeKey:@"aboutUser"];
        [HUD setOnViewWithTitle:@"加载中" animated:YES];
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            [HUD hideFromViewAnimated:YES];
            [self.tableView footerEndRefreshing];
            [self.slimeView endRefresh];
            if (error) {
                if (self.records.count == 0) {
                    promptView.viewPrompt.hidden = NO;
                }
            }else{
                if (self.currentPage == 0) {
                    [self.records removeAllObjects];
                }
                for (MLIncomeRecord *withdraw in objects) {
                    [self.records addObject:withdraw];
                }
                if (self.records.count == 0) {
                    promptView.viewPrompt.hidden = NO;
                }else{
                    self.currentPage++;
                    promptView.viewPrompt.hidden = YES;
                    [self.tableView reloadData];
                }
               
            }
            
        }];
    }
    if (str) {
        if (!promptView) {
            promptView = [[Utils alloc]init];
            [promptView viewControllerPrompt:CGRectMake(0, 64, DeviceWidth, DeviceHeight-64) Text:[NSString stringWithFormat:@"还没有%@",str] Image:[UIImage imageNamed:@"lianren_nul"]];
            [self.view addSubview:promptView.viewPrompt];
            
        }
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.records.count;
}
- (NSString *)changeNSDate:(NSDate *)date
{
  //  NSDate *nDate = [date dateByAddingTimeInterval:8*60*60];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *morelocationString=[dateFormatter stringFromDate:date];
    return morelocationString;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    WalletRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[WalletRecordCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if ([self.title isEqualToString:@"提现记录"]){
        //
        MLWithdraw *mlwithdraw = (MLWithdraw *)[self.records objectAtIndex:indexPath.row];
        cell.titleLabel.text = [NSString stringWithFormat:@"提现%ld元",(long)mlwithdraw.sum];
       cell.timeLabel.text = [self changeNSDate:mlwithdraw.createdAt];
        switch (mlwithdraw.status) {
            case MLWithdrawStatus_SUCCEED:
                cell.statusLabel.text = @"提现成功";
                cell.statusLabel.textColor = RGBA(84, 214, 133, 1);
                break;
            case MLWithdrawStatus_FAILED:
                cell.statusLabel.text = @"提现失败";
                cell.statusLabel.textColor = RGBA(238, 69, 76, 1);
                break;
            default:
                cell.statusLabel.text = @"进行中";
                cell.statusLabel.textColor = RGBA(249, 179, 79, 1);
                break;
        }
    }else if([self.title isEqualToString:@"充值记录"]){
        //充值记录
        MLConsumeRecord *consumeRecord = (MLConsumeRecord *)[self.records objectAtIndex:indexPath.row];
        cell.titleLabel.text = [NSString stringWithFormat:@"充值%ld元",(long)(consumeRecord.money)];
        cell.timeLabel.text = [self changeNSDate:consumeRecord.createdAt];
        cell.statusLabel.text = @"充值成功";
        cell.statusLabel.textColor = RGBA(235, 97, 138, 1);
        
    }else if([self.title isEqualToString:@"收支记录"]){
        //充值记录
        MLIncomeRecord *incomeRecord = (MLIncomeRecord *)[self.records objectAtIndex:indexPath.row];
        MLUser *taUser = (MLUser *)incomeRecord.aboutUser;
        if (taUser &&taUser.nickname) {
            cell.titleLabel.text = [NSString stringWithFormat:@"%@  (%@)",incomeRecord.content,taUser.nickname];
        }else{
            cell.titleLabel.text = incomeRecord.content;
        }
        
        
        cell.timeLabel.text = [self changeNSDate:incomeRecord.createdAt];
        if(incomeRecord.price>0){
            cell.statusLabel.textColor = RGBA(74, 219, 100, 1);
        }else{
           cell.statusLabel.textColor = RGBA(254, 161, 107, 1);
        }
        cell.statusLabel.text = [NSString stringWithFormat:@"%ld金币",(long)(incomeRecord.price*10)];
        cell.statusLabel.textColor = RGBA(235, 97, 138, 1);
        
    }
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
